package org.budo.compress;

import org.budo.compress.rar.RarCompressor;
import org.budo.compress.sevenzip.SevenzipCompressor;
import org.budo.compress.tar.TarCompressor;
import org.budo.compress.tar.gz.TarGzipCompressor;
import org.budo.compress.zip.ZipCompressor;
import org.budo.support.lang.util.StringUtil;

/**
 * @author lmw
 */
public class CompressorFactory {
    public static AbstractCompressor getCompressor(String zipFileName) {
        if (null == zipFileName || zipFileName.isEmpty()) {
            throw new IllegalArgumentException("#14 zipFileName = " + zipFileName);
        }

        if (StringUtil.equalsIgnoreCase(zipFileName, "zip") || StringUtil.endWithIgnoreCase(zipFileName, ".zip")) {
            return new ZipCompressor();
        }

        if (StringUtil.equalsIgnoreCase(zipFileName, "rar") || StringUtil.endWithIgnoreCase(zipFileName, ".rar")) {
            return new RarCompressor();
        }

        if (StringUtil.equalsIgnoreCase(zipFileName, "tar") || StringUtil.endWithIgnoreCase(zipFileName, ".tar")) {
            return new TarCompressor();
        }

        if (StringUtil.equalsIgnoreCase(zipFileName, "7z") || StringUtil.endWithIgnoreCase(zipFileName, ".7z")) {
            return new SevenzipCompressor();
        }

        if (StringUtil.equalsIgnoreCase(zipFileName, "tar.gz") || StringUtil.equalsIgnoreCase(zipFileName, "tgz") //
                || StringUtil.endWithIgnoreCase(zipFileName, ".tar.gz") || StringUtil.endWithIgnoreCase(zipFileName, ".tgz")) {
            return new TarGzipCompressor();
        }

        throw new RuntimeException("#32 unsupported output format, zipFileName=" + zipFileName);
    }
}